<template>
  <div>
    <el-row style="margin: 10px">
      <el-col>
        <!--        <el-col :span="4">-->
        <!--          <el-input v-model="searchPhone" placeholder="通过用户手机号搜索" clearable />-->
        <!--        </el-col>-->
        <el-col :span="4" style="margin-left: 20px">
          <el-input v-model="searchName" placeholder="通过用户名搜索" clearable maxlength="15" />
        </el-col>
        <el-col :span="4" style="margin-left: 20px">
          <el-input v-model="searchRealName" placeholder="通过用户真实姓名搜索" clearable maxlength="5" />
        </el-col>
        <el-col :span="1" style="margin-left: 20px">
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-col>
      </el-col>
    </el-row>
    <el-table :data="listData" style="width: 100%">
      <!--      <el-table-column label="id" width="180" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span style="margin-left: 10px">{{ scope.row.id }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="真实姓名" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template slot-scope="scope">
          <span v-if="!scope.row.content">无</span>
          <el-popover v-else trigger="hover" placement="top">
            <p>内容: {{ scope.row.content }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.content.length>10?scope.row.content.substring(0,7)+'...':scope.row.content }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="反馈图片" align="center">
        <template slot-scope="scope">
          <span v-for="item in scope.row.img">
            <el-image :src="item" style="width: 40px; height: 40px; margin: 4px" :preview-src-list="scope.row.img" />
          </span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" width="240px" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px"><el-date-picker type="datetime" :value="scope.row.createTime" readonly /></span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.reply" trigger="hover" placement="top">
            <p>回复: {{ scope.row.reply }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.reply.length>10?scope.row.reply.substring(0,7)+'...':scope.row.reply }}</el-tag>
            </div>
          </el-popover>
          <el-button v-else size="mini" type="text" @click="handleReply(scope.$index, scope.row)">回复</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="12" :offset="14" style="margin-top: 20px">
      <el-pagination
        background
        layout="prev, pager, next, jumper, sizes"
        :current-page="page.currentPage"
        :total="page.total"
        :pager-count="5"
        :page-size="page.pageSize"
        :page-sizes="page.pageSizes"
        @current-change="pageChange"
        @size-change="sizeChange"
      />
    </el-col>
    <el-dialog title="反馈回复" :visible.sync="dialogVisible" width="30%" :before-close="resetForm" :rules="rules">
      <el-form ref="replyForm" :model="replyForm" :rules="rules">
        <el-form-item label="" prop="reply" style="margin-bottom: 0px">
          <el-col :span="24">
            <el-input v-model="replyForm.reply" type="textarea" :autosize="{ minRows: 4, maxRows: 6}" maxlength="200" />
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('replyForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getFeedbackList, reply } from '@/api/feedback'

export default {
  data() {
    return {
      listData: [],
      searchPhone: '',
      searchName: '',
      searchRealName: '',
      dialogVisible: false,
      page: {
        pageSize: 5,
        pageSizes: [5, 10, 20, 40, 80],
        totalPage: 1,
        total: 0,
        currentPage: 1
      },
      replyForm: {
        id: '',
        reply: ''
      },
      rules: {
        reply: [
          { required: true, message: '请输入回复', trigger: 'blur' },
          { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getFeedbackList({
        page: this.page.currentPage,
        limit: this.page.pageSize,
        searchPhone: this.searchPhone,
        searchName: this.searchName,
        searchRealName: this.searchRealName
      }).then((res) => {
        this.listData = JSON.parse(res.data.list)
        // 遍历数组中的img转为list
        for (let i = 0; i < this.listData.length; i++) {
          if (this.listData[i].img) {
            console.log(i)
            this.listData[i].img = JSON.parse(this.listData[i].img)
          }
        }
        this.page.total = res.data.total
        this.page.totalPage = res.data.totalPage
      })
    },
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },
    pageChange(page) {
      this.page.currentPage = page
      this.getList()
    },
    sizeChange(size) {
      this.page.pageSize = size
      this.getList()
    },
    handleReply(index, row) {
      this.replyForm.id = row.id
      this.dialogVisible = true
    },
    handleConfirm(replayForm) {
      this.$refs[replayForm].validate((valid) => {
        if (valid) {
          reply(this.replyForm).then((res) => {
            if (res.data) {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.getList()
              this.resetForm()
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
              this.resetForm()
            }
          })
        } else {
          this.$message({
            message: '格式错误',
            type: 'error'
          })
          this.resetForm()
          return false
        }
      })
      this.dialogVisible = false
    },
    resetForm() {
      this.replyForm.id = ''
      this.replyForm.reply = ''
    }
  }
}
</script>

<style scoped>

</style>
